package com.dq.controller.api.merchant.workbench.customer;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.request.ChangeWxUserOrgRequest;
import com.dq.controller.in.request.CustomerChangeRequest;
import com.dq.controller.in.WxUserChangeQuery;
import com.dq.vo.ListVo;
import com.dq.vo.Vo;

import com.dq.domain.org.Org;
import com.dq.domain.record.WxUserChangeRecord;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/11 10:29
 */
@RestController
@RequestMapping("/api/merchant/workbench/wxUser/change")
@Api(description = "顾客变更记录")
public class WxUserChangeRecordController extends AllService {

    @ApiOperation("变更顾客技师")
    @PostMapping("changeWxUserStaff")
    public Vo changeWxUserStaff(@RequestBody CustomerChangeRequest customerChangeRequest) throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,customerChangeRequest.getStaffUserId());
        if(staffUser == null){
            throw new AdminException("转移目标技师不能为空");
        }
        List<WxUser> list = wxUserRepository.findByIdIn(customerChangeRequest.getWxUserId());
        HashSet<WxUserChangeRecord> set = new HashSet<>();
        list.forEach( wxUser -> {
            try {
                WxUserChangeRecord wxUserChangeRecord = staffUserChangeRecordService.save(wxUser, staffUser, null, WxUserChangeRecord.WxUserChangeType.technician);
                set.add(wxUserChangeRecord);
            } catch (AdminException e) {
                e.printStackTrace();
            }
        });
        wxUserRepository.saveAll(list);
        wxUserChangeRecordRepository.saveAll(set);
        return new Vo(HttpStatus.OK);
    }
    @ApiOperation("变更顾客门店")
    @PostMapping("changeWxUserOrg")
    public Vo changeWxUserOrg(@RequestBody ChangeWxUserOrgRequest request) throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,request.getStaffUserId());
        if(staffUser == null){
            throw new AdminException("转移目标技师不能为空");
        }
        SysSection sysDept = dao.get(SysSection.class,request.getOrgId());
        if(sysDept == null){
            throw new AdminException("变更的门店不能为空");
        }
        List<WxUser> list = wxUserRepository.findByIdIn(request.getWxUserId());
        HashSet<WxUserChangeRecord> set = new HashSet<>();
        list.forEach( wxUser -> {
            wxUser.setSection(sysDept);
            try {
                WxUserChangeRecord wxUserChangeRecord = staffUserChangeRecordService.save(wxUser, staffUser, (Org) sysDept, WxUserChangeRecord.WxUserChangeType.org);
                set.add(wxUserChangeRecord);
            } catch (AdminException e) {
                e.printStackTrace();
            }
        });
        wxUserRepository.saveAll(list);
        wxUserChangeRecordRepository.saveAll(set);
        return new Vo(HttpStatus.OK);
    }
    @ApiOperation("顾客变更记录列表")
    @PostMapping("/get")
    public ListVo get(@RequestBody WxUserChangeQuery query){
        List<WxUserChangeRecord> records = wxUserChangeRecordService.findByWhere(query);
        Long total = wxUserChangeRecordService.countByWhere(query);
        List<WxUserChangeRecord.WxUserChangeRecordVo> list = new ArrayList<>();
        records.forEach(record ->{
            list.add(record.toStaffUserChangeRecordVo());
        });
        return new ListVo(list,total);
    }
}
